from django.db import models


# Create your models here.

class CommonModel(models.Model):
    """模型基类"""
    created_at = models.DateTimeField('注册时间', auto_now_add=True)
    updated_at = models.DateTimeField('最后修改时间', auto_now=True)

    class Meta:
        abstract = True # 抽象类不会生成对应的表


'''' # ORM模型test


class User(CommonModel):
    """ 用户模型 """
    name = models.CharField('姓名', max_length=64)
    sex = models.CharField('性别', max_length=1, choices=(
        ('1', '男'), ('0', '女')
    ), default='1')
    age = models.PositiveBigIntegerField('年龄', default=0)
    username = models.CharField('用户名', max_length=64, unique=True)
    password = models.CharField('密码', max_length=256)
    #  密码密文存储，比较长
    remark = models.CharField('备注', max_length=64, null=True, blank=True)
    email = models.EmailField('用户邮箱', max_length=64, null=True, blank=True)
    # 收藏问题 多对多
    collect_ques = models.ManyToManyField('Question')

    class Meta:
        db_table = 'user'


class Profile(CommonModel):
    """"用户详细信息， 与User 一对一"""
    user = models.OneToOneField('User', on_delete=models.CASCADE,
                                related_name='profile', db_column='user')
    # zhangsan = User()
    # zhangsan.Profile 拿到用户张三的反向引用的详细信息
    nickname = models.CharField('昵称', max_length=64)


class Manager(User):
    class Meta:
        proxy = True


class Question(CommonModel):
    """ 问题"""
    qa_name = models.CharField('问题名称', max_length=64)


class Answer(CommonModel):
    """ 答案 一对多"""
    question = models.ForeignKey(Question, on_delete=models.CASCADE,
                                 related_name='answers',
                                 verbose_name='关联的问题')
    content = models.TextField('答案内容', max_length=64)


class Classify(models.Model):
    """ 分类
    1酒水-2啤酒，3白酒
    """
    qa_name = models.CharField('问题名称', max_length=64)
    parent = models.ForeignKey('self', on_delete=models.CASCADE, related_name='children')

'''
""" CRUD """


class User(CommonModel):
    """ 用户基本信息 """
    # id不用指定
    USER_STATUS = (
        (1, '正常'),
        (0, '删除')
    )
    username = models.CharField('用户名', max_length=128, unique=True)
    password = models.CharField('密码', max_length=256)
    nickname = models.CharField('昵称', max_length=128, null=True, blank=True)
    avatar = models.ImageField('用户头像', upload_to='avatar/%Y%m', null=True, blank=True)
    status = models.SmallIntegerField('用户状态', default=1, choices=USER_STATUS)
    is_super = models.BooleanField('是否为超级用户', default=False)

    class Meta:
        db_table='accounts_users'

    def __str__(self):
        return 'User:{}'.format(self.username)
    # 模型管理器
    # users = models.Manager()


class Profile(CommonModel):
    """"用户详细信息"""
    SEX_CHOICES =(
        (0, '未知'),
        (1, '男'),
        (2, '女'),

    )
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='关联用户',
                                related_name='profile')

    username = models.CharField('用户名', max_length=128, unique=True)
    real_name = models.CharField('用户名', max_length=128, null=True, blank=True)
    sex = models.SmallIntegerField('性别', default=0, choices=SEX_CHOICES)
    maxim = models.CharField('格言', max_length=128)
    address =  models.CharField('用户地址', max_length=128)

    class Meta:
        db_table = 'accounts_users_profile'


class LoginHistory(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE,
                             verbose_name='关联用户', related_name='login_history_list')
    username = models.CharField('用户名', max_length=128)
    login_type = models.CharField('账号平台', max_length=128)
    ip = models.CharField('账号平台', max_length=32, default='')
    ua = models.CharField('登录来源', max_length=32, default='')
    created_at = models.DateTimeField('登录时间', auto_now_add=True)

    class Meta:
        db_table = 'accounts_login_history'
        ordering = ['-created_at']


class Grade(models.Model):
    """ 数据统计 """
    student_name = models.CharField('学生姓名', max_length=64)
    subject_name = models.CharField('科目', max_length=32)
    score = models.FloatField('分数', default=0)
    year = models.SmallIntegerField('年份')

    class Meta:
        db_table = 'grade'
